import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:fluro/fluro.dart';
//import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:ivc/utils/provider.dart';
import 'package:ivc/utils/shared_preferences.dart';
import 'package:ivc/views/home.dart';
//import 'model/search_history.dart';
import 'package:ivc/model/user_info.dart';
import 'package:ivc/event/event_bus.dart';
import 'package:ivc/event/event_model.dart';
import 'package:event_bus/event_bus.dart';
import 'package:ivc/routers/routers.dart';
import 'package:ivc/routers/application.dart'  show Application;



SpUtil sp;
var db;

class MyApp extends StatefulWidget {
  MyApp() {
    final router = new Router();
    Routes.configureRoutes(router);
    // 这里设置项目环境
    Application.router = router;
  }

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _hasLogin = false;
  bool _isLoading = true;
  UserInformation _userInfo;
  bool isConnected = false;
  String registrationId;
  List notificationList = [];
  int themeColor = 0xFFC91B3A;

  _MyAppState() {
    final eventBus = new EventBus();
    ApplicationEvent.event = eventBus;
  }

  @override
  void initState() {
    super.initState();
    setState(() {
      _hasLogin = true;
      _isLoading = false;
      _userInfo = new UserInformation();
    });
  }

  showWelcomePage() {
    if (_isLoading) {
      return Container(
        color: Color(this.themeColor),
        child: Text(
          "Welcome",
        ),
      );
    } else {
      // 判断是否已经登录
      if (_hasLogin) {
        return AppPage(_userInfo);
      } else {
        //return LoginPage();
        return AppPage(_userInfo);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
//    WidgetTree.getCommonItemByPath([15, 17], Application.widgetTree);
    return new MaterialApp(
      title: 'titles',
      theme: new ThemeData(
        primaryColor: Color(this.themeColor),
        backgroundColor: Color(0xFFEFEFEF),
        accentColor: Color(0xFF888888),
        textTheme: TextTheme(
          //设置Material的默认字体样式
          body1: TextStyle(color: Color(0xFF888888), fontSize: 16.0),
        ),
        iconTheme: IconThemeData(
          color: Color(this.themeColor),
          size: 35.0,
        ),
      ),
      home: new Scaffold(body: showWelcomePage()),
      debugShowCheckedModeBanner: false,
      onGenerateRoute: Application.router.generator,
//      navigatorObservers: <NavigatorObserver>[Analytics.observer],
    );
  }
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  /*
  final provider = new Provider();
  await provider.init(true);
   */
  sp = await SpUtil.getInstance();
//  new SearchHistoryList(sp);

//  db = Provider.db;
  runApp(new MyApp());
}
